// pages/myDiary/myDiary.js
const util = require("../../utils/util")
const order = require("../../apis/order")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 6,
    pages: 0,
    count: 0,
    info: "",
    noData: false,
    loading: false,
    beginTime: util.formatTime2(new Date()).split(" ")[0],
    endTime: util.formatTime2(new Date()).split(" ")[0],
    currentDate: new Date().getTime(),
    minDate: util.getBeginTime(),
    show: false,
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    }
  },
  choose(e) {
    const { type } = e.currentTarget.dataset
    this.setData({
      show: true,
      type: type,
    })
    if (type == 1) {
      this.setData({ currentDate: new Date(this.data.beginTime).getTime() })
    } else this.setData({ currentDate: new Date(this.data.endTime.getTime())})
  },
  onInput(event) {
    const { endTime, beginTime, type } = this.data
    if (type == 1) {
      if (event.detail >= new Date(endTime).getTime()) {
        wx.showToast({
          title: '开始时间不能大于结束时间',
          icon: "none"
        })
      } else {
        this.setData({
          beginTime: util.formatTime2(new Date(event.detail)).split(" ")[0],
        })
        this.onClose()
        this.init()
      }
    } else if (type == 2) {
      if (new Date(beginTime).getTime() >= event.detail) {
        wx.showToast({
          title: '开始时间不能大于结束时间',
          icon: "none"
        })
      } else {
        this.setData({
          endTime: util.formatTime2(new Date(event.detail)).split(" ")[0],
        })
        this.onClose()
        this.init()
      }
    }

  },
  onClose() {
    this.setData({
      show: false,
    });
  },

  //初始化
  async init(type) {
    if (!type) {
      wx.showLoading({
        title: '加载中...',
      })
    }
    const param = {
      state: 0, // 回收大厅只需要显示待接单状态的订单
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      beginTime: this.data.beginTime.split("-").join("/") + " 00:00:00",
      endTime: this.data.endTime.split("-").join("/") + " 23:59:59"
    }
    let result = await order.listOrder(param);
    if (result.code == 200) {
      this.setData({
        list:  result.data.content,
        pages: result.data.page,
        count: result.data.count
      })
      wx.hideLoading({
        success: (res) => { },
      })
    }
  },
  detail(e){
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?id='+id,
    })
  },
  //下一页
  next() {
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    this.init();
  },

  // 上一页
  pre() {
    this.setData({
      pageNum: this.data.pageNum - 1
    })
    this.init();
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const info = JSON.parse(wx.getStorageSync("userInfo"))
    this.setData({ info: info })
    const { windowHeight, windowWidth } = app.globalData
    const backHeight = windowHeight - (windowWidth / 750) * 2 * 35
    this.setData({
      backHeight: backHeight,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init()
    this.setData({index:-1})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})